package com.user.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.user.dao.QueryPageCoupon;
import com.user.dao.coupon.CreateCoupon;
import com.user.dao.coupon.MyCoupon;
import com.user.dao.coupon.ReceiveCoupon;
import com.user.dao.coupon.UserHaveCoupon;
import com.user.domain.Coupon;
import com.user.domain.UserCoupon;
import com.user.mapper.CouponMapper;
import com.user.mapper.UserCouponMapper;
import com.user.service.CouponService;
import com.user.utils.R;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author YU
 * @description 针对表【coupon】的数据库操作Service实现
 * @createDate 2024-05-19 10:48:07
 */
@Service
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon>
        implements CouponService {

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private UserCouponMapper userCouponMapper;

    @Override
    public R getCoupon(QueryPageCoupon queryPageCoupon) {
        //获取优惠价信息
        Page<Coupon> mapPage = new Page<>(queryPageCoupon.getCurrent(), queryPageCoupon.getSize(), false);
        IPage<Coupon> page = couponMapper.selectUser(queryPageCoupon, mapPage);
        List<Coupon> records = page.getRecords();
        page.setRecords(records);
        page.setTotal(couponMapper.selectUserCount(queryPageCoupon));
        return R.ok().setData(page);
    }

    @Override
    public R getUserNoCoupon(QueryPageCoupon queryPageCoupon) {
        //查询用户没有领取的优惠券、
        List<Integer> couponIds = userCouponMapper.selectUserCouponByUserId(queryPageCoupon.getUserId());
        //获取优惠价信息
        List<Coupon> page = couponMapper.selectUser(queryPageCoupon);
        page.removeIf(coupon -> couponIds.contains(coupon.getId()));
        return R.ok().setData(page);
    }

    @Override
    public R createCoupon(CreateCoupon createCoupon) {
        //创建优惠券
        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(createCoupon, coupon);
        coupon.setRId(1);
        int insert = couponMapper.insert(coupon);
        if (insert > 0) return R.ok();
        return R.error();
    }

    @Override
    public R getUser(Integer id) {
        //获取用户优惠券
        List<UserHaveCoupon> coupons = userCouponMapper.selectUserCoupon(id);
        return R.ok().setData(coupons);
    }

    @Override
    public R receiveCoupon(ReceiveCoupon receiveCoupon) {
        //查询优惠券是否已经领取
        Integer count = userCouponMapper.selectUserCouponByCouponIdAndUserId(receiveCoupon.getCouponId(), receiveCoupon.getUserId());
        if (count > 0) return R.error("优惠券已经领取");
        UserCoupon userCoupon = new UserCoupon();
        userCoupon.setCouponId(receiveCoupon.getCouponId());
        userCoupon.setUserId(receiveCoupon.getUserId());
        userCoupon.setStatus(0);
        userCouponMapper.insert(userCoupon);
        return R.ok();
    }

    @Override
    public R getMyCoupon(Integer userId) {
        //获取用户优惠券
        List<MyCoupon> coupons = userCouponMapper.selectMyCoupon(userId);
        return R.ok().setData(coupons);
    }
}




